#include "main.h"

PSP_MODULE_INFO("Harmonium", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(12*1024);

//Init OSLib
int initOSLib(){
    oslInit(0);
    oslInitGfx(OSL_PF_8888, 1);
    oslInitAudio();
    oslSetQuitOnLoadFailure(1);
    oslSetKeyAutorepeatInit(20);
    oslSetKeyAutorepeatInterval(5);
	oslSetTransparentColor(RGB(255,0,255));
    return 0;
}

//Load intraFont fonts
void loadFont(){
	OSL_FONT* ltn[16];
    char file[40];
    int k;
    for (k = 0; k < 16; k++) {
        sprintf(file, "flash0:/font/ltn%d.pgf", k);
        ltn[k] = oslLoadFontFile(file);
        oslIntraFontSetStyle(ltn[k], 1.0f,WHITE,BLACK,INTRAFONT_ALIGN_LEFT);
    }
}

//Main grid
int grid[sizeX][sizeY];

void initGrid(int sizeX, int sizeY) {
	int i;
	int j;
	for (i = 0; i < sizeX; i++) {
		for (j = 0; j < sizeY; j++) {
			grid[i][j] = 0;
		}
	}
}

void setupGrid(int sizeX, int sizeY, int lenghtX, int lenghtY, int offsetX, int offsetY) {
	int i;
	int j;
	for(i = 0; i < sizeX; i++) {
		for(j = 0; j < sizeY; j++) {
			if (grid[i][j] == 2) {
				oslDrawImageXY(selector, sX * lenghtX + offsetX, sY * lenghtY + offsetY);
			} else if (grid[i][j] == 1) {
				oslDrawImageXY(full, i * lenghtX + offsetX, j * lenghtY + offsetY);
			} else if(grid[i][j] == 0) {
				oslDrawImageXY(empty, i * lenghtX + offsetX, j * lenghtY + offsetY);
			}
		}
	}
}

//Main loop
int main(){
	initOSLib();

	oslIntraFontInit(INTRAFONT_CACHE_ALL | INTRAFONT_STRING_UTF8); 
	loadFont();

	background = oslLoadImageFilePNG("background.png", OSL_IN_RAM, OSL_PF_8888);
	empty = oslLoadImageFilePNG("empty.png", OSL_IN_RAM, OSL_PF_8888);
	selector = oslLoadImageFilePNG("selector.png", OSL_IN_RAM, OSL_PF_8888);
	full = oslLoadImageFilePNG("full.png", OSL_IN_RAM, OSL_PF_8888);

	initGrid(sizeX, sizeY);

	while (!osl_quit) {
        if (!skip){

			oslStartDrawing();
			oslDrawImage(background);

			setupGrid(sizeX, sizeY, lenghtX, lenghtY, offsetX, offsetY);
			grid[sX][sY] = 2;

			if (osl_pad.pressed.cross) {
				grid[sX][sY] = 1;
			}
		}

		oslReadKeys();

		if (osl_pad.pressed.right && sX < sizeX) {
				if(grid[sX + 1][sY] == 0) {
					sX += 1;
					grid[sX][sY] = 2;
					grid[sX - 1][sY] = 0;
				}
				if(grid[sX + 1][sY] == 1) {
					sX += 1;   
					grid[sX][sY] = 2;   
					grid[sX - 1][sY] = 1;
				}
		}
		if (osl_pad.pressed.left && sX > 0) {
				if(grid[sX - 1][sY] == 0) {
					sX -= 1;
					grid[sX][sY] = 2;
					grid[sX + 1][sY] = 0;
				}
				if(grid[sX - 1][sY] == 1) {
					sX -= 1;
					grid[sX][sY] = 2;
					grid[sX + 1][sY] = 1;
				}
		}
		if (osl_pad.pressed.down && sY < 7) {
				if(grid[sX][sY + 1] == 0 && sY != 8) {
					sY += 1;
					grid[sX][sY] = 2;
					grid[sX][sY - 1] = 0;
				}
				if(grid[sX][sY + 1] == 1) {
					sY += 1;
					grid[sX][sY] = 2;
					grid[sX][sY - 1] = 1;
				}
		}
		if (osl_pad.pressed.up && sY > 0) {
				if (grid[sX][sY - 1] == 0) {
					sY -= 1;
					grid[sX][sY] = 2;
					grid[sX][sY + 1] = 0;
				}
				if (grid[sX][sY - 1] == 1) {
					sY -= 1;
					grid[sX][sY] = 2;
					grid[sX][sY + 1] = 1;
				}
		}

		oslEndDrawing();
		oslEndFrame();
        skip = oslSyncFrame();

	}

	//Quit OSLib:
    oslEndGfx();
    sceKernelExitGame();
    return 0;
}